package com.apes.enquiry.brand.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.enquiry.brand.model.SpBrand;
import com.apes.enquiry.brand.repository.SpBrandRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.scm.masterdata.product.model.Brand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 类描述：
 *
 * @ClassName SpBrandService
 * @Description 描述
 * @Author wushilong
 * @Date 2023/7/6 17:14
 * @Version 1.0
 */
@Service
public class SpBrandService extends DomainService {

    @Autowired
    private SpBrandRepository spBrandRepository;

    /**
     * 同步品牌
     * @param request
     */
    public void saveBrand(SimpleRequest request){
        JSONObject data = request.getJO();
        JSONArray brands = data.getJSONArray("data");
        for (Object o: brands) {
            JSONObject brand = (JSONObject) o;
            SpBrand spBrand = spBrandRepository.findOne(brand.getString("PPID"));
            if (spBrand == null) {
                spBrand = new SpBrand();
                spBrand.setId(brand.getString("PPID"));
            }
            spBrand.setName(brand.getString("PPMC"));
            spBrand.setEnName(brand.getString("YWMC"));
            spBrand.setBrandTypeId(brand.getString("PPLXID"));
            spBrand.setBrandTypeName(brand.getString("PPLXMC"));
            spBrand.setQualityId(brand.getString("PZID"));
            spBrand.setQualityName(brand.getString("PZMC"));
            spBrand.setTime(brand.getString("TS01"));
            spBrand.setCreatorName(brand.getString("LRR"));
            spBrand.setSubdivideBrandId(brand.getString("XFPZID"));
            spBrand.setSubdivideBrandName(brand.getString("XFPZMC"));
            spBrand.setEntryDate(brand.getDate("LRRQ_SJ"));
            spBrandRepository.saveAndFlush(spBrand);
        }

    }
}
